<%@page contentType="text/html" pageEncoding="utf-8"%>
<!doctype html>
<html>
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge" />
		<link href="../css/main.css" type="text/css" rel="stylesheet" />
		<link rel="stylesheet" href="../css/zTreeStyle/zTreeStyle.css" type="text/css">
		<link href="../css/dialog.css" rel="stylesheet" type="text/css">
		<title>企业书屋运营管理平台</title>
	</head>
	<body>
		<div class="main_frame">
			<div class="position_div">
				<img src="../images/position_icon.png" />
				<i>系统管理</i><i>&gt;&gt;</i><i>角色管理</i>
			</div>
			<div class="right_div">
				<div class="search_div">
					<div class="label_div">
						<label class="text_label">
							角色名：
						</label>
						<input type="text" class="input_search" placeholder="支持模糊查询"  id="role_name" />
					</div>
					<div class="done_div">
						<button class="btn_common1 title_div2" type="button" id="submit_btn" onclick="qryRoleListByCondition()">
							<i></i>搜索
						</button>
						<button style="margin-left:10px !important" class="btn_common2 newadd" id="add_role" type="button" onclick="addRole()">
							<i></i>新增
						</button>
					</div>
                    <div class="cl">&nbsp;</div>
				</div>
				<table class="table_list" id="tab1">
					<tr>
						<th width="20%">
								角色名称
							</th>
							<th width="20%">
								创建时间
							</th>
							<th width="20%">
								角色备注
							</th>
							<th width="20%">
								菜单权限
							</th>
							<th class="last_td">
								基本操作
							</th>
					</tr>
					<tbody id="listbox1">
					</tbody>
				</table>
				
				
				<!-- 添加角色 -->
			   <div class="right_content_edit" id="addRoleBox">
				    <div class="blank_div"></div>
				    <label class="text_label label_width">角色名称：</label>
				    <input type="text" class="input_search width_input" id="rolename"/>
				    			    &nbsp;&nbsp;<span style="color: red">*</span>
				     <div class="blank_div"></div>
				    <label class="text_label label_width">角色备注：</label>
				    <input type="text" class="input_search width_input" id="nameremarks"/>
				    <div class="blank_div"></div>
				    <label class="text_label label_width">菜单权限：</label>
				    <div class="content_wrap">
						<ul id="treeDemo1" class="ztree"></ul>
					</div>
					<div class="blank_div"></div>
					<div class="blank_div"></div>
			    	<div class="bottom_donebtn width_div">
			    		<button class="btn_common2" onclick="doAddRole()">提交</button>
			    		<button class="btn_common1" onclick="edit_cancel(1)">取消</button>
			    	</div>
			  	</div>
			  	
			  	<!-- 修改角色 -->
			   <div class="right_content_edit" id="editRoleBox">
			   		<input type="hidden" name="edit_id" id="edit_id" />
			   		<input type="hidden" name="viewmenuid" id="viewmenuid" />
				    <div class="blank_div"></div>
				    <label class="text_label label_width">角色名称：</label>
				    <input type="text" class="input_search width_input" id="edit_rolename"/>
						&nbsp;&nbsp;<span style="color: red">*</span>
				     <div class="blank_div"></div>
				    <label class="text_label label_width">角色备注：</label>	
				    <input type="text" class="input_search width_input" id="edit_nameremarks"/>
				    <div class="blank_div"></div>
				    <label class="text_label label_width">菜单权限：</label>
				    <div class="content_wrap">
						<ul id="treeDemo" class="ztree"></ul>
					</div>
					
					<div class="blank_div"></div>
					<div class="blank_div"></div>
			    	<div class="bottom_donebtn width_div">
			    		<button class="btn_common2" id="editRoleBtn" onclick="doEditRole()">提交</button>
			    		<button class="btn_common1" onclick="edit_cancel(2)">取消</button>
			    	</div>
			  	</div>
				<!--系统弹出框-->
				<div class="opacity_div"></div>   
				<div class="prop_div">
				 <div class="prop_top"><i>查看菜单权限</i>
				 	<a href="javascript:void(0)" class="close_icon"><img src="../images/close_icon.png"/></a>
				 </div>
				 <div class="content_div">
				    <!--树形结构-->
				    <div class="content_wrap">
						<ul id="treeDemo2" class="ztree"></ul>
					</div>
					<button class="btn_pop cancel_btn">关闭</button>
				 </div>
				 
				</div>
				
				<!--分页 -->
				<div class="turn_page" style="display:none">
				   <div class="main_page" id="pageCon">
					   <a href="javascript:void(0)" class="first_a" id="firstPage"></a>
					   <a href="javascript:void(0)" class="up_a" id="prev"></a>    	
					   <a href="javascript:void(0)" class="next_a" id="next"></a>
					   <a href="javascript:void(0)" class="last_a" id="lastPage"></a>
					   <input id="page_num_input" style="width:40px;" type="text" onkeyup="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}"/>
					   <button type="button" class="num_input" id="go_num_input">跳转</button>
					   <b class="page_text">当前<i><em id="nowpage"></em>/<em id="totalpage"></em></i>页，总数<i id="totalNum"></i>条</b>    	
				   </div>
				</div>
			</div>
		</div>
		<script type="text/javascript" src="../js/jquery-1.10.2.min.js"></script>
		<script type="text/javascript" src="../js/common.js"></script>
		<script type="text/javascript" src="../js/dialog.js"></script>
		<script type="text/javascript" src="../js/jquery.ztree.core-3.5.js"></script>
		<script type="text/javascript" src="../js/jquery.ztree.excheck-3.5.js"></script>
		<script>
		var yema=1;
		var pagelen=10;
		var totalPage=0;
		setting = {
				check: {
					enable: true
				},
				data: {
					simpleData: {
						enable: true
					}
				}
			};
		$(document).ready(function() {
			qryRoleList();
		});
		//查询按钮触发事件
		function qryRoleListByCondition() {
			yema=1;
			qryRoleList();
		}
		//获取列表
		function qryRoleList() {
			var role_name = $("#role_name").val();
			$.ajax({
				url: 'getRoleList.do', 
				type: 'POST',
				data:{roleName:role_name,yema:yema,pagelen:pagelen}, 
				async: false,
				dataType: 'json', 
				timeout: 10000, 
				error: function(){LQWarn('请求失败,请稍后重试');}, 
				success: function(result){
					console.log(result.resultCode);
					if(result.resultCode==1000){
						var items=result.returnObject.pageList;
						console.log(items);
						if (items&& items.length > 0) {
						
							var pageMsg=result.returnObject.pageMsg;
							var rlist="";
							$.each(items,function(i){
								var item = items[i];
								/* rlist+='<tr><td>'+item.ROLENAME+'</td><td>'+format(item.CREATETIME)+'</td><td>'+replaceNull(item.REMARKS)+'</td><td><a onclick="qryRoleMenu(this)">查看</td><td class="last_td"><input type="hidden" value='+item.ID+' name="roleID"/><input type="hidden" value='+item.menuIDs+' name="menuIDs"/><button class="edit_btn btn_motify" title="修改" onclick="editRole(this)">修改</button><button class="edit_btn btn_delete" title="删除" onclick="delRole('+item.ID+')">删除</button></td></tr>'; */
								rlist+='<tr><td>'+item.ROLENAME+'</td><td>'+format(item.CREATETIME)+'</td><td>'+replaceNull(item.REMARKS)+'</td><td><a class="look" onclick="qryRoleMenu(this)">查看</a></td><td class="last_td"><input type="hidden" value='+item.ID+' name="roleID"/><input type="hidden" value='+item.menuIDs+' name="menuIDs"/><p class="mod"  onclick="editRole(this)">修改</p><p class="del" onclick="delRole('+item.ID+')">删除</p></td></tr>';
							});
							$("#listbox1").html(rlist);
							
							totalPage=result.returnObject.totalPage;
							$("#nowpage").html(result.returnObject.curPage);
							$("#totalpage").html(totalPage);
							$("#totalNum").html(result.returnObject.totalNum);
							$(".turn_page").show();
						}else{
							$("#listbox1").html("");
							$(".turn_page").hide();
						}
					}
					else if(result.resultCode==1004){
						LQWarn(result.returnObject);
						window.top.location.href ="../login.jsp";
					}else{
						//LQWarn(result.returnObject);
					}
				}
			});
		}
		
		
		$("#firstPage").on("click",function(){
			yema=1;
			qryRoleList();		
		});
		$("#lastPage").on("click",function(){
			yema=totalPage;
			qryRoleList();
		});
		$("#prev").on("click",function(){
			if(yema>1){
				yema=yema-1;
				qryRoleList();
			}
		});
		$("#next").on("click",function(){
			if(yema<totalPage){
				yema=yema+1;
				qryRoleList();
			}
		});
		$("#go_num_input").on("click",function(){
			var page_num = $("#page_num_input").val();
			if(page_num == ""){
				LQWarn("请先输入页码");
				return;
			}
			if(page_num-1<totalPage){
				yema=parseInt(page_num);
				qryRoleList();
			}else{
				LQWarn("不能超出总页数");
			}
		});
		//删除角色
		function delRole(role_id){
			LQConfirm("您确定要删除该角色吗？",function(){
				$.ajax({
					url: 'delRole.do', 
					type: 'POST',
					data:{roleID:role_id}, 
					async: false,
					dataType: 'json', 
					timeout: 10000, 
					error: function(){LQWarn('请求失败,请稍后重试');}, 
					success: function(result){
						if(result.resultCode==1000){
							//LQMsg("删除成功");
							qryRoleList();
						}
						else if(result.resultCode==1004){
							LQWarn(result.returnObject);
							window.top.location.href ="../login.jsp";
						}else{
							LQWarn(result.returnObject);
						}
					}
				});	
			});
		}
		//显示添加页面-隐藏主页面
		function addRole(){
			$("#rolename").val("");
			//判断权限后打开修改页面
			
			//获取可分配的菜单权限
			$.ajax({
				url: 'getMenuList.do', 
				type: 'POST',
				data:{menuID:"",menuName:"",navMenu:"",yema:1,pagelen:2000}, 
				async: false,
				dataType: 'json', 
				timeout: 10000, 
				error: function(){LQWarn('请求失败,请稍后重试');}, 
				success: function(result){
					if(result.resultCode==1000){
						var items=result.returnObject.pageList;
						if (items.length > 0) {
							ztreeList=items;
							var zNodes =[];			
							$.each(ztreeList,function(i){
								var node={};
								node.id=ztreeList[i].ID;
								node.pId=ztreeList[i].PARENTID;
								node.name=ztreeList[i].MENUNAME;
								node.open=true;
								zNodes.push(node);						
							});
							$.fn.zTree.init($("#treeDemo1"), setting, zNodes);
							$(".search_div,.table_list").hide();
							$(".turn_page").hide();
							$("#addRoleBox").show();
						}else{
							LQWarn("无可分配菜单权限");
						}
					}else if(result.resultCode==1004){
						LQWarn(result.returnObject);
						window.top.location.href ="../login.jsp";
					}else{
						LQWarn(result.returnObject);
					}
				}
			});
		}
		//显示修改页面-隐藏主页面
		function editRole(ob){
			$(".search_div").hide();
			//判断权限后打开修改页面
			$.ajax({
				url: 'editRole.do', 
				type: 'POST',
				async: false,
				dataType: 'json', 
				timeout: 10000, 
				error: function(){LQWarn('请求失败,请稍后重试');}, 
				success: function(result){
					if(result.resultCode==1000){
						var _this=$(ob),
						tr=_this.parents("tr");
						$("#edit_rolename").val(tr.find('td').eq(0).text());
						$("#edit_nameremarks").val(tr.find('td').eq(2).text()=="查看" ? "" : tr.find('td').eq(2).text());
						$("#edit_id").val(tr.find("input[name=roleID]").val());
						var menuSelected=tr.find("input[name=menuIDs]").val().split(",");
						//获取可分配的菜单权限
						$.ajax({
							url: 'getMenuList.do', 
							type: 'POST',
							data:{menuID:"",menuName:"",navMenu:"",yema:1,pagelen:2000}, 
							async: false,
							dataType: 'json', 
							timeout: 10000, 
							error: function(){LQWarn('请求失败,请稍后重试');}, 
							success: function(result){
								if(result.resultCode==1000){
									var items=result.returnObject.pageList;
									//可视菜单
									var viewmenuid=[];
									if (items.length > 0) {
										ztreeList=items;
										var zNodes =[];			
										$.each(ztreeList,function(i){
											var node={};
											node.id=ztreeList[i].ID;
											viewmenuid.push("'"+ztreeList[i].ID+"'");
											node.pId=ztreeList[i].PARENTID;
											node.name=ztreeList[i].MENUNAME;
											node.open=true;
											//比对是否选中
											for(var j=0;j<menuSelected.length;j++){
												if(ztreeList[i].ID==menuSelected[j]){
													node.checked=true;
												}
											}
											zNodes.push(node);						
										});
										$("#viewmenuid").val(viewmenuid.join(","));
										$.fn.zTree.init($("#treeDemo"), setting, zNodes);
									}else{
										LQWarn("无可分配菜单权限");
									}
								}else if(result.resultCode==1004){
									LQWarn(result.returnObject);
									window.top.location.href ="../login.jsp";
								}else{
									LQWarn(result.returnObject);
								}
							}
						});
						
						_this.parents(".search_div").hide().siblings(".turn_page").hide();
						$(".table_list,.turn_page").hide();
						$("#editRoleBox").show();
					}
					else if(result.resultCode==1004){
						LQWarn(result.returnObject);
						window.top.location.href ="../login.jsp";
					}else{
						LQWarn(result.returnObject);
					}
				}
			});	
		}
		
		//修改页面取消按钮事件
		function edit_cancel(etype){
			$(".search_div,.table_list").show();
			$(".turn_page").show();
			if(etype==1){
				$("#addRoleBox").hide();
			}else if(etype==2){
				$("#editRoleBox").hide();
			}
		}
		
		//编辑提交按钮
		function doEditRole(){
			var rolename = $("#edit_rolename").val();
			var viewmenuid = $("#viewmenuid").val();
			var roleID = $("#edit_id").val();
			var edit_nameremarks = $("#edit_nameremarks").val();
			menus=[];
			var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
			var checked = treeObj.getCheckedNodes(true);
			$.each(checked,function(i){
				var id=checked[i].id;
				menus.push(id);
			});
			var menus_str=menus.join(",");
			if(rolename==""){
				LQWarn("请输入角色名称");
				return;
			}
			if(menus_str==""){
				LQWarn("请选择菜单权限");
				return;
			}
			$.ajax({
				url: 'doEditRole.do', 
				type: 'POST',
				async: false,
				data:{viewmenuid:viewmenuid,roleID:roleID,roleName:rolename,remarks:edit_nameremarks,menus:menus_str}, 
				dataType: 'json', 
				timeout: 10000, 
				error: function(){LQWarn('请求失败,请稍后重试');}, 
				success: function(result){
					if(result.resultCode==1000){
						LQMsg("修改成功");
						edit_cancel(2);
						qryRoleList();
					}else if(result.resultCode==1004){
						LQWarn(result.returnObject);
						top.location.href ="../login.jsp";
					}else{
						LQWarn(result.returnObject);
					}
				}
			}); 
		}
		//添加
		function doAddRole(){
			var rolename = $.trim($("#rolename").val());
			var remarks = $("#nameremarks").val();
			menus=[];
			var treeObj = $.fn.zTree.getZTreeObj("treeDemo1");
			var checked = treeObj.getCheckedNodes(true);
			$.each(checked,function(i){
				var id=checked[i].id;
				menus.push(id);
			});
			var menus_str=menus.join(",");
			
			if(rolename==""){
			    $("#rolename").focus();
				LQWarn("请输入角色名称");
				return;
			}
			if(menus_str==""){
				LQWarn("请选择菜单权限");
				return;
			}
			$.ajax({
				url: 'doAddRole.do', 
				type: 'POST',
				async: false,
				data:{roleName:rolename,remarks:remarks,menus:menus_str}, 
				dataType: 'json', 
				timeout: 10000, 
				error: function(){LQWarn('请求失败,请稍后重试');}, 
				success: function(result){
					
					if(result.resultCode==1000){
						LQMsg("添加成功");
						edit_cancel(1);
						qryRoleList();
					}else if(result.resultCode==1004){
						LQWarn(result.returnObject);
						top.location.href ="../login.jsp";
					}else{
						LQWarn(result.returnObject);
					}
				}
			}); 
		}
		//点击查看用户权限
		function qryRoleMenu(ob){
			var setting2 = {
					check: {
						enable: false
					},
					data: {
						simpleData: {
							enable: true
						}
					}
				}
			var _this=$(ob),
			tr=_this.parents("tr");
			var menuSelected=tr.find("input[name=menuIDs]").val().split(",");
			//获取可分配的菜单权限
			$.ajax({
				url: 'getMenuList.do', 
				type: 'POST',
				data:{menuID:"",menuName:"",navMenu:"",yema:1,pagelen:2000}, 
				async: false,
				dataType: 'json', 
				timeout: 10000, 
				error: function(){LQWarn('请求失败,请稍后重试');}, 
				success: function(result){
					if(result.resultCode==1000){
						var items=result.returnObject.pageList;
						//可视菜单
						var viewmenuid=[];
						if (items.length > 0) {
							ztreeList=items;
							var zNodes =[];			
							$.each(ztreeList,function(i){
								var node={};
								node.id=ztreeList[i].ID;
								viewmenuid.push("'"+ztreeList[i].ID+"'");
								node.pId=ztreeList[i].PARENTID;
								node.name=ztreeList[i].MENUNAME;
								node.open=true;
								//比对是否选中
								for(var j=0;j<menuSelected.length;j++){
									if(ztreeList[i].ID==menuSelected[j]){
										zNodes.push(node);
									}
								}
							});
							$("#viewmenuid").val(viewmenuid.join(","));
							$.fn.zTree.init($("#treeDemo2"), setting2, zNodes);
							$(".opacity_div").show();
							$(".prop_div").show();
						}else{
							LQWarn("你无可查看菜单权限");
						}
					}else if(result.resultCode==1004){
						LQWarn(result.returnObject);
						window.top.location.href ="../login.jsp";
					}else{
						LQWarn(result.returnObject);
					}
				}
			});
		}
		</script>
	</body>
</html>
